package com.lancoo.skegndemo;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.lancoo.skegn.LancooSkEgnManager;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = "MainActivity";
    protected Toast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
        //判断是否为android6.0系统版本，如果是，需要动态添加权限
        if (Build.VERSION.SDK_INT >= 23) {
            verifyPermissions(this);
            //  Log.e("6.0以上的版本---->", "获取权限");
        } else {
            // Log.e("6.0以下的版本---->", "不需获取权限");
            initSkegn();
        }
        findViewById(R.id.btn_pingce).setOnClickListener(this);
        findViewById(R.id.btn_shibie).setOnClickListener(this);
    }


    public void verifyPermissions(Activity activity) {
        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO)
                != PackageManager.PERMISSION_GRANTED
                || ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED
                || ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getApplicationContext(), "没有获取权限,请手动开启权限", Toast.LENGTH_SHORT).show();
            // 申请一个（或多个）权限，并提供用于回调返回的获取码（用户定义）
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 100);//,Manifest.permission.READ_PHONE_STATE
        }else{
            initSkegn();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 100:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 获取到权限，作相应处理（调用定位SDK应当确保相关权限均被授权，否则可能引起定位失败）
                    initSkegn();
                } else {
                    // 没有获取到权限，做特殊处理
                    Toast.makeText(getApplicationContext(), "获取应用权限失败，请手动开启，或重装应用", Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
        }
    }

    private void initSkegn(){
        LancooSkEgnManager.getInstance().initSkegn(this, new LancooSkEgnManager.OnSkEgnInitListener() {
            @Override
            public void onStartInitEngine() {
                Log.e(TAG, "onStartInitEngine: ");
                toast.setText("开始初始化打分引擎");
                toast.show();
            }

            @Override
            public void onInitEngineSuccess() {
                Log.e(TAG, "onInitEngineSuccess: ");
                toast.setText("初始化打分引擎成功");
                toast.show();
            }

            @Override
            public void onInitEngineFailed() {
                Log.e(TAG, "onInitEngineFailed: " );
                toast.setText("初始化打分引擎失败");
                toast.show();
            }
        });
    }

    @Override
    public void onClick(View view) {
        Intent mIntent=new Intent();
        switch (view.getId()){
            case R.id.btn_pingce:
                mIntent.setClass(this,EvaluationActivity.class);
                break;
            case R.id.btn_shibie:
                mIntent.setClass(this,DiscemmentActivity.class);
                break;
        }
        startActivity(mIntent);
    }
}
